﻿// Decompiled with JetBrains decompiler
// Type: System.Web.Razor.Parser.WhiteSpaceRewriter
// Assembly: System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// MVID: 682513D6-F37E-48AC-9909-F3EE103CFF14
// Assembly location: D:\Project\Idea\无人机外网\uav\客户端工程\排查工具\bin\Release\System.Web.Razor.dll

using System.Collections.Generic;
using System.Linq;
using System.Web.Razor.Parser.SyntaxTree;
using System.Web.Razor.Text;

namespace System.Web.Razor.Parser
{
  internal class WhiteSpaceRewriter : MarkupRewriter
  {
    public WhiteSpaceRewriter(
      Action<SpanBuilder, SourceLocation, string> markupSpanFactory)
      : base(markupSpanFactory)
    {
    }

    protected override bool CanRewrite(System.Web.Razor.Parser.SyntaxTree.Block block) => block.Type == BlockType.Expression && this.Parent != null;

    protected override SyntaxTreeNode RewriteBlock(BlockBuilder parent, System.Web.Razor.Parser.SyntaxTree.Block block)
    {
      BlockBuilder blockBuilder = new BlockBuilder(block);
      blockBuilder.Children.Clear();
      Span original = block.Children.FirstOrDefault<SyntaxTreeNode>() as Span;
      IEnumerable<SyntaxTreeNode> syntaxTreeNodes = block.Children;
      if (original.Content.All<char>(new Func<char, bool>(char.IsWhiteSpace)))
      {
        SpanBuilder builder = new SpanBuilder(original);
        builder.ClearSymbols();
        this.FillSpan(builder, original.Start, original.Content);
        parent.Children.Add((SyntaxTreeNode) builder.Build());
        syntaxTreeNodes = block.Children.Skip<SyntaxTreeNode>(1);
      }
      foreach (SyntaxTreeNode syntaxTreeNode in syntaxTreeNodes)
        blockBuilder.Children.Add(syntaxTreeNode);
      return (SyntaxTreeNode) blockBuilder.Build();
    }
  }
}
